package com.imodia.checkoo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.hsq.commons.util.tree.TreeNodeBean;
import com.imodia.checkoo.bean.CmsBaseSourceBean;
import com.imodia.checkoo.bean.CmsDepartmentBean;
import com.imodia.checkoo.bean.CmsDepartmetCatalogueBean;
import com.imodia.checkoo.bean.CmsManagerMenuBean;
import com.imodia.checkoo.service.CmsDataDictionaryService;
import com.imodia.checkoo.service.CmsDepartmetCatalogueService;
import com.imodia.checkoo.service.impl.CmsDepartmentServiceImpl;
import com.imodia.checkoo.service.impl.CmsManagerMenuServiceImpl;
import com.imodia.checkoo.service.impl.CmsUserServiceImpl;

@Controller
@RequestMapping("/hfmanager/department/list.do")
public class CmsDepartmentController extends BaseAdminController{
	
	private static final Log log = LogFactory.getLog(CmsDepartmentController.class);

	private final String baseUrl = "department";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";
	private final String url_list4acticle = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list4acticle";
	private final String url_menu_set = "/hfmanager/" + baseUrl + "/menu_set";

	@Autowired
	private CmsDepartmentServiceImpl cmsDepartmentService;
	@Autowired
	private CmsManagerMenuServiceImpl cmsManagerMenuService;
	@Autowired	
	private CmsDepartmetCatalogueService cmsDepartmetCatalogueService;
	@Autowired
	private CmsUserServiceImpl cmsUserService;
	@Autowired
	private CmsDataDictionaryService CmsDataDictionaryService;
	
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsDepartmentService.delete(id);
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsDepartmentBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsDepartmentService.update(obj);
		}else{
			x = cmsDepartmentService.insert(obj);
			obj.setId(x);
		}
		
		if(x>0){
			List<CmsDepartmetCatalogueBean> menuList = new ArrayList<CmsDepartmetCatalogueBean>();
			String[] menuArray = request.getParameterValues("menuList");
			if(menuArray!=null){
				for(int i=0;i<menuArray.length;i++){
					CmsDepartmetCatalogueBean dc = new CmsDepartmetCatalogueBean();
					dc.setCatagolueId(Integer.parseInt(menuArray[i]));
					dc.setDepartmentId(obj.getId());
					dc.setTypeId(1);
					menuList.add(dc);
				}
			}
			cmsDepartmetCatalogueService.save(menuList,obj.getId(),1);
			
			List<CmsDepartmetCatalogueBean> departmentCatalist = new ArrayList<CmsDepartmetCatalogueBean>();
			String[] areaArray = request.getParameterValues("areaList");
			if(areaArray!=null){
				for(int i=0;i<areaArray.length;i++){
					CmsDepartmetCatalogueBean dc = new CmsDepartmetCatalogueBean();
					dc.setCatagolueId(Integer.parseInt(areaArray[i]));
					dc.setDepartmentId(obj.getId());
					dc.setTypeId(3);
					departmentCatalist.add(dc);
				}
			}
			cmsDepartmetCatalogueService.save(departmentCatalist,obj.getId(),3);
			
			String userLists = request.getParameter("userLists");
			if(userLists!=null){
				cmsUserService.clear(obj.getId());
				String[] userListArray = userLists.split(",");
				for(int i=0;i<userListArray.length;i++){
					if(userListArray[i].matches("\\d+")){
						cmsUserService.updateDepartMent(obj.getId(), Integer.parseInt(userListArray[i]));
					}
				}
			}
		}
		if(x>0)x=1;
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect+"&x="+x+"&id="+obj.getId()), null);
	}
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request,CmsDepartmentBean obj) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();

		m.put("info", cmsDepartmentService.get(id));
		
		List<TreeNodeBean> treeInfo = null;
		if(super.getAdminId(request)==1){
			treeInfo = cmsManagerMenuService.getManageMenuAll();
		}else{
			treeInfo = cmsManagerMenuService.getManageMenuAll2();
		}
		
		cmsDepartmetCatalogueService.setSelect(id, treeInfo,1);
		
		m.put("treeInfo", treeInfo);
		
		m.put("userInfo", cmsUserService.query(id));
		
		List<TreeNodeBean> areaList = CmsDataDictionaryService.getTreePage(9);
		cmsDepartmetCatalogueService.setSelect(id, areaList,3);
		m.put("areaList", areaList);
		
	    return new ModelAndView(url_edit, m);
	}
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsDepartmentBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		SqlParam sql = new SqlParam();
		if(super.getAdminId(request)!=1){
			sql.andNotEqualTo("id","1");
		}
		PageResult<CmsDepartmentBean> pageResult = cmsDepartmentService.page(new SqlParam(),page);
		model.put("page", pageResult);
		return new ModelAndView(url_list, model);
	}
	
	@RequestMapping(params = "action=list4acticle")
	public ModelAndView list4acticle(HttpServletRequest request, CmsDepartmentBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
	
		
		PageResult<CmsDepartmentBean> pageResult = cmsDepartmentService.page(new SqlParam(),page);
		model.put("page", pageResult);
		
		return new ModelAndView(url_list4acticle, model);
	}
	
	
	@RequestMapping(params = "action=departmentMenuSet")
	public ModelAndView departmentMenuSet(HttpServletRequest request, CmsManagerMenuBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		model.put("info", cmsManagerMenuService.getManageMenu(0));
		return new ModelAndView(url_menu_set, model);
	}
}
